package at.studentinformation;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.database.Cursor;

public class Subject {
	public String SubjectId;
	public String SubjectName;
	public String SubjectViName;    
	public int Credit;
    
    public static Subject ExamineFromJsonObject(JSONObject post)
	{
    	Subject result = new Subject();
		try
        {
            result.SubjectId = post.getString("SubjectId");
            result.SubjectName = post.getString("SubjectName");
            result.SubjectViName = post.getString("SubjectViName");
            result.Credit = Integer.parseInt(post.getString("Credit"));
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
        }
		return result;
	}
	
	public static ArrayList<Subject> GetListSubjectFromJson(String input)
	{
		ArrayList<Subject> result = new ArrayList<Subject>();
		try
        {   			
            JSONArray entries = new JSONArray(input);
            for(int i =0; i<entries.length(); i++)
            {            	
            	JSONObject post = entries.getJSONObject(i);
            	Subject temp = Subject.ExamineFromJsonObject(post);
	            result.add(temp);
            }
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
        }
		return result;	
	}

	public static Subject GetSubjectByCusor(Cursor c)
	{
		Subject result = new Subject();
		result.SubjectId = c.getString(c.getColumnIndex("SubjectId"));
		result.SubjectName = c.getString(c.getColumnIndex("SubjectName"));
		result.SubjectViName = c.getString(c.getColumnIndex("SubjectViName"));
		result.Credit = Integer.parseInt(c.getString(c.getColumnIndex("Credit")));
		
		return result;
	}

	public static ArrayList<Subject> GetListSubjectFormCursor(Cursor c)
	{
		ArrayList<Subject> result = new ArrayList<Subject>();
		while(c.moveToNext())
		{
			Subject temp = Subject.GetSubjectByCusor(c);
			result.add(temp);
		}
		return result;
	}
}
